/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dialog;

import main.Demo;
import main.EnumerateWindows;
import dto.Application;
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.JDialog;
import jni.DDEClientUrlBrowser;
import util.DateUtil;
import util.StringUtil;
import xml.XmlFile;

/**
 *
 * @author phat10130059
 */
public class RunBlockWindow extends javax.swing.JPanel {

    /**
     * Creates new form RunBlockWindow
     */
    public Demo jframe;
    Toolkit toolkit;
    Timer timer;
    public boolean isRunning = false;
    public List<Application> list;
    public String user;

    public RunBlockWindow(JDialog jdialog, Demo jframe) {
        initComponents();

        this.jframe = jframe;

        user = System.getProperty("user.name").trim().toLowerCase();

//        this.jframe.running = true;
//        this.jframe.getCurrentWindow();

        list = XmlFile.readFileXml("file.xml");

        toolkit = Toolkit.getDefaultToolkit();
        timer = new Timer();
        timer.schedule(new scheduleDailyTask1(), 1000, 3000);

//        this.jframe.setVisible(false);



    }

    class scheduleDailyTask1 extends TimerTask {

        public void run() {
            if (isRunning) {
                getCurrentWindow();
            }


        }
    }

    public void getCurrentWindow() {

//        System.out.println("==================================");

        jTextField1.setText(EnumerateWindows.activeWindowTitle());

        String activeWindowTitle = EnumerateWindows.activeWindowTitle().toLowerCase();
        String activeWindowProcess = EnumerateWindows.activeWindowProcess().toLowerCase();

        if (activeWindowProcess.equals("chrome.exe")
                || activeWindowProcess.equals("firefox.exe")
                || activeWindowProcess.equals("iexplore.exe")) {

            for (Application application : list) {
                if (application.getType().equals("appTile")) {
                    if (application.getUser().toLowerCase().equals(user)
                            || application.getUser().equals("[All]")) {
                        if (application.getStart().trim().equals("")
                                && application.getEnd().trim().equals("")) {

                            String title = StringUtil.joinString(StringUtil.cutString(activeWindowTitle));
                            if (title.contains(application.getWindow().toLowerCase())) {
                                try {
                                    EnumerateWindows.closeTabBrowser();
                                    return;
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                }
                            }
                        } else {
                            if (DateUtil.checkTime(application.getStart(), application.getEnd())) {
                                String title = StringUtil.joinString(StringUtil.cutString(activeWindowTitle));
                                if (title.contains(application.getWindow().toLowerCase())) {
                                    try {
                                        EnumerateWindows.closeTabBrowser();
                                        return;
                                    } catch (Exception ex) {
                                        ex.printStackTrace();
                                    }
                                }
                            }

                        }
                    }
                }

            }
        } else {

            for (Application application : list) {
                if (activeWindowTitle.contains(application.getWindow().toLowerCase())) {

                    if (application.getType().equals("appTile")) {

                        if (application.getUser().toLowerCase().equals(user)
                                || application.getUser().equals("[All]")) {

                            if (application.getStart().trim().equals("")
                                    && application.getEnd().trim().equals("")) {
                                try {
                                    if (EnumerateWindows.isProcessRunging(activeWindowProcess)) {
//                            System.out.println("ok");
                                        EnumerateWindows.killProcess(activeWindowProcess);
                                        return;
//                                break;
                                    }
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                }
                            } else {
                                if (DateUtil.checkTime(application.getStart(), application.getEnd())) {
                                    try {
                                        if (EnumerateWindows.isProcessRunging(activeWindowProcess)) {
//                            System.out.println("ok");
                                            EnumerateWindows.killProcess(activeWindowProcess);
                                            return;
//                                break;
                                        }
                                    } catch (Exception ex) {
                                        ex.printStackTrace();
                                    }
                                }
                            }


                        }
                    }

                }
            }

        }

        if (EnumerateWindows.activeWindowProcess().toLowerCase().equals("firefox.exe")
                || EnumerateWindows.activeWindowProcess().toLowerCase().equals("iexplore.exe")) {
            if (jframe.cbBlockWeb.isSelected()) {
                String url = DDEClientUrlBrowser.getUrlBrowser(StringUtil.cutExe(EnumerateWindows.activeWindowProcess().toLowerCase()).get(0));
                if (url.contains("https://") || url.contains("http://")) {
                    boolean n = false;
                    boolean t = false;
                    for (Application application : list) {
                        if (application.getType().equals("site")) {
                            if (application.getUser().toLowerCase().equals(user)
                                    || application.getUser().equals("[All]")) {
                                if (application.getStart().trim().equals("")
                                        && application.getEnd().trim().equals("")) {
                                    t = true;
                                    if (url.contains(application.getWindow().toLowerCase())) {
                                        n = true;
                                        break;
                                    }
                                } else {
                                    if (DateUtil.checkTime(application.getStart(), application.getEnd())) {
                                        t = true;
                                        if (url.contains(application.getWindow().toLowerCase())) {
                                            n = true;
                                            break;
                                        }
                                    }
                                }

                            }
                        }

                    }



//                    System.out.println("phat"+n);

                    if ((n == false) && (t == true)) {
                        try {
                            EnumerateWindows.closeTabBrowser();
                            return;
                        } catch (AWTException ex) {
                            ex.printStackTrace();
                        }
                    }


                }
            }


        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Block", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Active Window:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(98, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addContainerGap(129, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
